package cn.etgps.etgps.ui;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import cn.etgps.etgps.R;
import cn.etgps.etgps.base.BaseActivity;
import cn.etgps.etgps.blmanager.TruckManager;
import cn.etgps.etgps.dataEntity.TruckDetailBean;
import cn.etgps.etgps.dataEntity.TruckDetailEntity;
import cn.etgps.etgps.dialog.DialogCreater;
import cn.etgps.etgps.utils.Constant;
import cn.etgps.etgps.view.TitleBar;

/**
 * 车辆详情类
 */
public class TruckDetailActivity extends BaseActivity implements View.OnClickListener {
    private TitleBar mTitlebarView = null;
    /**
     * 变量存储有适配器传来的vid
     */
    private int mVid = 0;
    /**
     * 车辆号码
     */
    private TextView mTruckNumber = null;
    /**
     * 车辆类型
     */
    private TextView mTruckType = null;
    /**
     * 司机姓名
     */
    private TextView mDeiverName = null;
    /**
     * 司机电话
     */
    private TextView mDeiverPhone = null;
    /**
     * 设备编号
     */
    private TextView mDeviceNumber = null;
    /**
     * 设备类型
     */
    private TextView mDeviceType = null;
    /**
     * SIM编号
     */
    private TextView mSimNumber = null;
    /**
     * 开始时间
     */
    private TextView mStartTime = null;
    /**
     * 结束时间
     */
    private TextView mEndTime = null;
    /**
     * 发送信息
     */
    private Button mSendMessage = null;
    /**
     * 拨打电话
     */
    private Button mCall = null;
    /**
     * 车辆详情对象
     */
    private TruckDetailBean mBean = null;
    private TruckManager mUserManger = null;
    /**
     * 车辆详情请求线程
     */
    private DetailTask mDetailTask = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_truck_detail);
        mTitlebarView = (TitleBar) findViewById(R.id.title_bar);
        mTitlebarView.initTitleBarInfo(getString(R.string.label_marker_car_information), "", R.drawable.icon_return, "",
                0, false);
        mTitlebarView.setMainClickListener(this, getString(R.string.btn_cancel), "");
        initView();
    }

    private void initView() {
        if (getIntent() != null) {
            mVid = getIntent().getIntExtra("vid", -1);
        }
        mBean = new TruckDetailBean();
        mUserManger = new TruckManager(this);
        mTruckNumber = (TextView) findViewById(R.id.tv_truckNumber);
        mTruckType = (TextView) findViewById(R.id.type_detal);
        mDeiverName = (TextView) findViewById(R.id.truckName);
        mDeiverPhone = (TextView) findViewById(R.id.truckPhone);
        mDeviceNumber = (TextView) findViewById(R.id.deviceNumber);
        mDeviceType = (TextView) findViewById(R.id.deviceType);
        mSimNumber = (TextView) findViewById(R.id.simNumber);
        mStartTime = (TextView) findViewById(R.id.startTime);
        mEndTime = (TextView) findViewById(R.id.dueTime);
        mSendMessage = (Button) findViewById(R.id.sendMessage);
        mCall = (Button) findViewById(R.id.callPhone);
        mSendMessage.setOnClickListener(this);
        mCall.setOnClickListener(this);
        initData();
    }

    private void initData() {
        if (mUserManger.checkNetwork(this)) {
            if (mDetailTask != null && mDetailTask.getStatus() == AsyncTask.Status.RUNNING) {
                mDetailTask.cancel(true);
            }
            mDetailTask = new DetailTask();
            mDetailTask.execute();
        } else {
            mUserManger.showToastMessage(getString(R.string.toast_no_network), 1000);
        }
    }

    @Override
    public void clickButtonListener(String name) {
        if (name.equals(getString(R.string.btn_cancel))) {
            this.finish();
        }
    }

    @Override
    public void onClick(View v) {
        String number = mBean.getTel();
        switch (v.getId()) {
            case R.id.sendMessage:
                if (number != null && (!number.isEmpty())) {
                    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + number));
                    startActivity(intent);
                } else {
                    mUserManger.showToastMessage(getString(R.string.phone_empty), 1000);
                }
                break;
            case R.id.callPhone:
                if (number != null && (!number.isEmpty())) {
                    Intent phone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                    checkCallingPermission(Manifest.permission.CALL_PHONE);
                    startActivity(phone);
                } else {
                    mUserManger.showToastMessage(getString(R.string.phone_empty), 1000);
                }
                break;
        }
    }

    private void setData(TruckDetailBean info) {
        this.mBean = info;
        mTruckNumber.setText(info.getLicense());
        mTruckType.setText(info.getVehicleTypeName());
        mDeiverName.setText(info.getDriverName());
        mDeiverPhone.setText(info.getTel());
        mDeviceNumber.setText(info.getAvl_no());
        mDeviceType.setText(info.getAvl_type());
        mSimNumber.setText(info.getSimNO());
        mStartTime.setText(info.getAvl_time());
        mEndTime.setText(info.getAvlEndTime());
    }

    /**
     * 开启请求数据线程
     */
    public class DetailTask extends AsyncTask<String, Void, TruckDetailEntity> {

        @Override
        protected TruckDetailEntity doInBackground(String... params) {
            showNormalDialog(DialogCreater.DIALOG_PROGRESS);
            if (mVid == -1) {
                return null;
            } else {
                String vd = String.valueOf(mVid);
                return mUserManger.detailTruckFromServer(vd);
            }
        }

        @Override
        protected void onPostExecute(TruckDetailEntity s) {
            super.onPostExecute(s);
            dismissNormalDialog();
            if (s.getStatus().equals(Constant.RETURN_RESULT_OK)) {
                setData(s.getContent());
            } else if (s.getStatus().equals(Constant.JSON_DETE_ERROR)) {
                mUserManger.showToastMessage(getString(R.string.message_error), 1000);
            } else {
                mUserManger.showToastMessage(getString(R.string.toast_connect_server_failed), 1000);
            }
        }
    }
}
